﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Majestic12;
using System.Net;
using System.IO;
using System.Reflection;


namespace SmartMovieTime
{
    public partial class QueryForm : Form
    {

        private List<AtMovieAreaRow> areaRowList;
        private string dataDir = null;
        private Dictionary<string, List<AtMovieTheaterRow>> subareaNTheater;
        private List<AtMovieTheaterRow> theaterRowList;
        private string areaTableFile;

        public QueryForm()
        {
            InitializeComponent();

            string localDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            dataDir = localDir + "\\data\\";
            areaTableFile = AtMovieTimeExtractor.GetAreaTableFile(dataDir);

            Singleton<HTMLparser>.Instance();

            areaRowList = null;
            subareaNTheater = null;
            theaterRowList = null;

            if (!Directory.Exists(dataDir))
            {
                Directory.CreateDirectory(dataDir);
            }

            if (!File.Exists(areaTableFile))
            {
                ConfigForm config = new ConfigForm(dataDir);
                config.ShowDialog();
            }

            LoadArea();
        }

        private void QueryForm_Closed(object sender, EventArgs e)
        {
            Singleton<HTMLparser>.Close();
        }

        private void menuUpdate_Click(object sender, EventArgs e)
        {
            ConfigForm config = new ConfigForm(dataDir);

            //If updated successfully, refresh the screen.
            if (config.ShowDialog() == DialogResult.OK)
            {
                ClearArea();
                ClearSubarea();
                ClearTheater();
                LoadArea();
                cbArea.Focus();
            }

        }

        private void menuLookup_Click(object sender, EventArgs e)
        {
            if (cbArea.SelectedIndex != -1 && cbTheater.SelectedIndex != -1)
            {
                MovieTimeForm movieForm = new MovieTimeForm(
                    dataDir,
                    areaRowList[cbArea.SelectedIndex].AreaId,
                    theaterRowList[cbTheater.SelectedIndex].TheaterId,
                    theaterRowList[cbTheater.SelectedIndex].TheaterName
                    );
                movieForm.ShowDialog();
            }
        }

        private void menuClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearSubarea();
            ClearTheater();
            menuLookup.Enabled = false;


            if (cbArea.SelectedIndex >= 0)
            {
                LoadSubarea(areaRowList[cbArea.SelectedIndex].AreaId);
            }
        }

        private void cbSubarea_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearTheater();
            menuLookup.Enabled = false;

            if (cbSubarea.SelectedIndex >= 0)
            {
                string subarea = (string)cbSubarea.Items[cbSubarea.SelectedIndex];
                LoadTheater(subarea);
            }
        }

        private void cbTheater_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTheater.SelectedIndex >= 0)
            {
                menuLookup.Enabled = true;
            }
            else
            {
                menuLookup.Enabled = false;
            }
        }     

        private void LoadArea()
        {
            areaRowList = new List<AtMovieAreaRow>();
                     
            if (File.Exists(areaTableFile))
            {
                //Extract area information
                TxtTable<AtMovieAreaRow> areaTable = new TxtTable<AtMovieAreaRow>();
                areaTable.LoadFromFile(areaTableFile);
                AtMovieAreaRow areaRow = null;
                while ((areaRow = areaTable.ReadNext()) != null)
                {
                    if (areaRow.Enabled)
                    {
                        cbArea.Items.Add(areaRow.AreaName);
                        areaRowList.Add(areaRow);
                    }
                }
                cbArea.Enabled = true;
                areaTable.Close();
            }
        }

        private void ClearArea()
        {
            cbArea.Items.Clear();
            cbArea.SelectedIndex = -1;
            cbArea.Enabled = false;
            if (areaRowList != null)
            {
                areaRowList.Clear();
                areaRowList = null;
            }
        }

        private void LoadSubarea(string areaId)
        {
            subareaNTheater = new Dictionary<string, List<AtMovieTheaterRow>>();
            //Extract movie time
            
            string theaterTableFile = AtMovieTimeExtractor.GetTheaterTableFile(dataDir, areaId);
            if (File.Exists(theaterTableFile))
            {
                TxtTable<AtMovieTheaterRow> theaterTable = new TxtTable<AtMovieTheaterRow>();
                theaterTable.LoadFromFile(theaterTableFile);
                AtMovieTheaterRow row;
                while ((row = theaterTable.ReadNext()) != null)
                {
                    if (subareaNTheater.ContainsKey(row.SubareaName))
                    {
                        subareaNTheater[row.SubareaName].Add(row);
                    }
                    else
                    {
                        List<AtMovieTheaterRow> rowList = new List<AtMovieTheaterRow>();
                        rowList.Add(row);
                        subareaNTheater.Add(row.SubareaName, rowList);
                    }
                }

                foreach (string subarea in subareaNTheater.Keys)
                {
                    cbSubarea.Items.Add(subarea);
                }
                cbSubarea.Enabled = true;
                theaterTable.Close();
            }
        }

        private void ClearSubarea()
        {
            cbSubarea.Items.Clear();        
            cbSubarea.SelectedIndex = -1;
            cbSubarea.Enabled = false;
            if (subareaNTheater != null)
            {
                subareaNTheater.Clear();
                subareaNTheater = null;
            }
        }

        private void LoadTheater(string subarea)
        {
            if (subareaNTheater != null)
            {
                if (subareaNTheater.ContainsKey(subarea))
                {
                    theaterRowList = subareaNTheater[subarea];

                    foreach (AtMovieTheaterRow row in theaterRowList)
                    {
                        cbTheater.Items.Add(row.TheaterName);
                    }

                    cbTheater.Enabled = true;
                }
            }
        }

        private void ClearTheater()
        {
            cbTheater.Items.Clear();
            cbTheater.SelectedIndex = -1;
            cbTheater.Enabled = false;
            theaterRowList = null;
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            AboutForm about = new AboutForm();
            about.ShowDialog();
        }

        
    }
}